package engine

import (
	"fmt"
	"sync"

	"gitee.com/zfd81/dbrs/sql/parser"

	"gitee.com/zfd81/dbrs/config"

	"github.com/go-redis/redis/v8"
	"vitess.io/vitess/go/sqltypes"
	querypb "vitess.io/vitess/go/vt/proto/query"
)

type OracleProvider struct {
	name   string
	typ    Type
	client redis.Cmdable
	mu     *sync.RWMutex
}

func (p *OracleProvider) Name() string {
	return p.name
}

func (p *OracleProvider) Type() Type {
	return p.typ
}

func (p *OracleProvider) Insert(ctx *Context, stmt *parser.InsertStatement) (*sqltypes.Result, error) {
	return &sqltypes.Result{}, nil
}

func (p *OracleProvider) Delete(ctx *Context, stmt *parser.DeleteStatement) (*sqltypes.Result, error) {
	return &sqltypes.Result{}, nil
}

func (p *OracleProvider) Update(ctx *Context, stmt *parser.UpdateStatement) (*sqltypes.Result, error) {

	return &sqltypes.Result{}, nil
}

func (p *OracleProvider) Select(ctx *Context, bindVars map[string]*querypb.BindVariable, callback func(*sqltypes.Result) error, stmt *parser.SelectStatement) error {
	r := &sqltypes.Result{
		Fields: []*querypb.Field{},
		Rows:   [][]sqltypes.Value{},
	}
	callback(r)
	return nil
}

func (p *OracleProvider) ShowTables(ctx *Context, stmt *parser.ShowStatement) (*sqltypes.Result, error) {
	r := &sqltypes.Result{}
	return r, nil
}

func (p *OracleProvider) ShowCharset(ctx *Context, stmt *parser.ShowStatement) (*sqltypes.Result, error) {
	return nil, fmt.Errorf("Currently redis engine does not support 'show charset' statement")
}
func (p *OracleProvider) ShowCollation(ctx *Context, stmt *parser.ShowStatement) (*sqltypes.Result, error) {
	return nil, fmt.Errorf("Currently redis engine does not support 'show collation' statement")
}

func (p *OracleProvider) DescTable(ctx *Context, stmt *parser.DescStatement) (*sqltypes.Result, error) {
	r := &sqltypes.Result{}
	return r, nil
}

func (p *OracleProvider) AlterTable(ctx *Context, stmt *parser.AlterTableStatement) (*sqltypes.Result, error) {
	return &sqltypes.Result{
		RowsAffected: 1,
	}, nil
}

func (p *OracleProvider) DropTable(ctx *Context, stmt *parser.DropTableStatement) (*sqltypes.Result, error) {
	return &sqltypes.Result{}, nil
}

func (p *OracleProvider) Client() redis.Cmdable {
	return p.client
}

func NewOracleProvider(db config.Database) (*OracleProvider, error) {
	return &OracleProvider{
		name: db.Name,
		typ:  Type_Oracle,
		mu:   &sync.RWMutex{},
	}, nil
}
